const express = require("express");
const router = express.Router();
const { User } = require("../../models");
const { Op } = require("sequelize");
const { NotFound } = require('http-errors');
const { success, failure } = require("../../utils/responses");

/**
 *获取用户列表.
 */
router.get("/", async function (req, res) {
  try {
    const query = req.query;
    const currentPage = Math.abs(Number(query.currentPage)) || 1;
    const pageSize = Math.abs(Number(query.pageSize)) || 10;
    const offset = (currentPage - 1) * pageSize;
    const condition = {
      where: {},
      order: [["id", "DESC"]],
      limit: pageSize,
      offset: offset,
    };

    if (query.email) {
      condition.where.email = query.email;
    }

    if (query.username) {
      condition.where.username = query.username;
    }

    if (query.nickname) {
      condition.where.nickname = {
        [Op.like]: `%${query.nickname}%`,
      };
    }

    if (query.role) {
      condition.where.role = query.role;
    }

    const { count, rows } = await User.findAndCountAll(condition);
    success(res, "获取用户列表成功", {
      users: rows,
      pagination: {
        total: count,
        currentPage,
        pageSize,
      },
    });
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 查询当前登录的用户详情
 * GET /admin/users/me
 */
router.get("/me", async function (req, res) {
  try {
    const user = req.user;
    success(res, "查询当前用户信息成功。", { user });
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 获取用户详情
 */
router.get("/:id", async function (req, res) {
  try {
    const user = await getUser(req);
    success(res, "获取用户成功", { user });
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 更新用户
 */
router.put("/:id", async function (req, res) {
  try {
    const user = await getUser(req);
    const body = filterBody(req);
    if (user) {
      await user.update(body);
      success(res, "修改用户成功", { user });
    } else {
      failure(res, new NotFound(`ID: ${id} 的用户未找到`));
    }
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 添加用户
 */
router.post("/", async function (req, res) {
  try {
    const body = filterBody(req);
    const user = await User.create(body);
    success(res, "创建用户成功", { user }, 201);
  } catch (error) {
    // 捕获创建用户时的错误
    failure(res, error);
  }
});

/**
 * 删除用户
 */
router.delete("/:id", async function (req, res) {
  try {
    const user = await getUser(req);
    await user.destroy();
    success(res, "删除用户成功");
  } catch (error) {
    failure(res, error);
  }
});

/**
 * 公共方法：白名单过滤
 * @param req
 * @returns {{password, role: (number|string|*), introduce: ({type: *}|*), sex: ({allowNull: boolean, type: *, validate: {notNull: {msg: string}, notEmpty: {msg: string}, isIn: {args: [number[]], msg: string}}}|{defaultValue: number, allowNull: boolean, type: *}|*), nickname: (string|*), company: ({type: *}|*), avatar: ({type: *, validate: {isUrl: {msg: string}}}|*), email: (string|*), username}}
 */
function filterBody(req) {
  return {
    email: req.body.email,
    username: req.body.username,
    password: req.body.password,
    nickname: req.body.nickname,
    sex: req.body.sex,
    company: req.body.company,
    introduce: req.body.introduce,
    role: req.body.role,
    avatar: req.body.avatar,
  };
}

/**
 * 公共方法：查询当前用户
 */
async function getUser(req) {
  // 获取用户ID
  const { id } = req.params;
  // 查询用户
  const user = await User.findByPk(id);

  // 如果用户不存在，抛出错误
  if (!user) {
    throw new NotFound(`ID: ${id} 的用户未找到`);
  }

  return user;
}

module.exports = router;
